const fs = require('fs');
const path = require('path');

module.exports = function (pagePath, name) {
  const routePath = pagePath + '/route';
  if (!fs.existsSync(routePath)) {
    fs.mkdirSync(routePath);
  }
  const routeName = name.replace(/\w/, a => a.toLowerCase());

  const routeFile = routePath + `/index.js`;
  if (!fs.existsSync(routeFile)) {
    const template = [];
    template.push(`export const ${name}Route = {`);
    template.push(`  path: '${routeName}',`);
    template.push(`  name: '',`);
    template.push(`  getComponent: (nextState, cb) => {`);
    template.push(`    require.ensure([], (require) => {`);
    template.push(`      cb(null, require('../index')['default']);`);
    template.push(`    });`);
    template.push(`  },`);
    template.push(`  childRoutes: []`);
    template.push(`};`);
    template.push(``);
    fs.writeFileSync(routeFile, template.join('\n'));
  }

}
